<template>
	<up-popup :show="show" closeable round="30rpx" @close="closePop">
		<view class="user-info-container pb-30">
			<view class="title pt-30">完善个人资料</view>
			<view class="desc">请完善您的个人信息，如不完善将使用默认</view>
			<view class="info-item mt-50 rounded p-30 flex items-center">
				<button class="avatar-btn flex" open-type="chooseAvatar" @chooseavatar="uploadAvatar">
					<image :src="profileRef.photo ? profileRef.photo : profile.photo" class="avatar flex-1"></image>
				</button>
				<view class="flex-1 ml-30">
					<view class="label">头像</view>
					<input class="inp" disabled placeholder="当前为默认头像，点击头像可修改" type="text" />
				</view>
			</view>
			<view class="info-item mt-30 rounded p-30 flex items-center">
				<view class="avatar-btn">
					<image class="avatar" src="./nickname.png"></image>
				</view>
				<view class="flex-1 ml-30">
					<view class="label">用户名</view>
					<input
						:value="profileRef.nickname ? profileRef.nickname : profile.nickname"
						class="inp"
						placeholder="请输入您的用户名昵称"
						type="nickname"
						@change="changeNickname"
					/>
				</view>
			</view>
			<view class="btn" @click="updateProfile">完成</view>
		</view>
	</up-popup>
</template>

<script setup>
	import { defineEmits, defineProps, ref } from 'vue';
	import { goUrl, hint, imgUrl, port, uploadDirectImage, loading, closeLoading } from '/service/port.js';

	const emits = defineEmits(['update:show', 'submit']);
	const props = defineProps({
		show: Boolean,
		profile: Object
	});

	const profileRef = ref({});

	async function uploadAvatar({ detail }) {
		console.log(detail);
		const url = detail.avatarUrl;
		const res = await uploadDirectImage(detail.avatarUrl);
		// const res = await uploadImg();
		profileRef.value.photo = res;
	}

	function changeNickname({ detail }) {
		profileRef.value.nickname = detail.value;
	}

	function closePop() {
		emits('update:show', false);
	}

	async function updateProfile(params) {
		loading();
		const res = await port('saveUser', {
			photo: profileRef.value.photo ? profileRef.value.photo : props.profile.default_photo,
			nickname: profileRef.value.nickname
		});
		closeLoading();
		hint(res.msg);
		emits('update:show', false);
		emits('submit');
	}
</script>

<style lang="scss" scoped>
	.btn {
		background: linear-gradient(-90deg, #fb3204 0%, #fe7800 100%);
		border-radius: 44rpx;
		padding: 20rpx 0;
		text-align: center;
		color: #fff;
		margin: 30rpx 30rpx 0 30rpx;

		> text {
			font-weight: 500;
			font-size: 30rpx;
			color: #ffffff;
		}
	}
	.p-30 {
		padding: 24rpx;
	}
	.pt-30 {
		padding-top: 24rpx;
	}
	.pb-30 {
		padding-bottom: 24rpx;
	}
	.user-info-container {
		.title {
			text-align: center;
			font-weight: 500;
			font-size: 32rpx;
		}

		.desc {
			margin: 12rpx 0;
			text-align: center;
			font-size: 24rpx;
			color: #999999;
		}

		.info-item {
			margin-left: 60rpx;
			margin-right: 60rpx;
			background-color: #fff8ef;
		}

		.avatar-btn {
			// width: 100rpx;
			// height: 100rpx;
			border-radius: 50%;
			padding: 0;
			line-height: inherit;
			.avatar {
				width: 100rpx;
				height: 100rpx;
			}
		}

		.label {
			font-weight: 500;
			font-size: 30rpx;
			color: #000000;
		}

		.inp {
			margin-top: 10rpx;
			font-size: 22rpx;
		}
	}
</style>
